package cn.yangliu.nacos.comm.config.security.handler;

import cn.yangliu.nacos.comm.model.Oauth2Token;
import cn.yangliu.nacos.comm.model.Oauth2UpdatePasswordModel;
import cn.yangliu.nacos.comm.model.Oauth2UpdateSelfModel;
import cn.yangliu.nacos.comm.model.Oauth2UserModel;
import cn.yangliu.nacos.comm.oauth.Oauth2Service;
import cn.yangliu.nacos.comm.oauth.TokenHandler;
import cn.yangliu.nacos.comm.tools.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2020 -01-01
 */
@Api(value = "用户个人接口", tags = "用户个人接口")
@RestController
@RequestMapping("/personal")
@ConditionalOnClass(Authentication.class)
@ConditionalOnProperty(prefix = "security.commom", name = "enable", havingValue = "true", matchIfMissing = true)
public class PersonalController {

    /**
     * current class instance's member.
     * The Token handler.
     */
    @Autowired
    private TokenHandler tokenHandler;

    /**
     * current class instance's member.
     * The Oauth 2 service.
     */
    @Autowired
    private Oauth2Service oauth2Service;

    /**
     * Update oauth 2 user model.
     *
     * @param model the model
     * @return the oauth 2 user model
     */
    @PutMapping
    @ApiOperation(value = "修改个人信息", tags = "用户个人接口")
    public Oauth2UserModel update(@RequestBody @Validated Oauth2UpdateSelfModel model) {
        Oauth2UserModel currentUser = SecurityUtils.getCurrentUser();
        String username = currentUser.getUsername();
        Oauth2Token oauth2Token = tokenHandler.load(username);
        return oauth2Service.updateSelf(model, oauth2Token.getAccessToken());
    }

    /**
     * Update password boolean.
     *
     * @param model the model
     * @return the boolean
     */
    @PutMapping("/password")
    @ApiOperation(value = "修改个人密码", tags = "用户个人接口")
    public boolean updatePassword(@RequestBody @Validated  Oauth2UpdatePasswordModel model) {
        Oauth2UserModel currentUser = SecurityUtils.getCurrentUser();
        String username = currentUser.getUsername();
        Oauth2Token oauth2Token = tokenHandler.load(username);
        return oauth2Service.updateSelfPassword(model.getPassword(),
                oauth2Token.getAccessToken());
    }

    /**
     * Principal object.
     *
     * @param authentication the authentication
     * @return the object
     */
    @GetMapping("/principal")
    @ApiOperation(value = "获取个人信息", tags = "用户个人接口")
    public Object principal(@ApiIgnore Authentication authentication) {
        return authentication.getPrincipal();
    }
}
